package com.ymatou.iapi.hunter.testcase;

import java.math.BigDecimal;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.hunter.paramater.UpdateHunterStatusRequest;
import com.ymatou.iapi.hunter.service.UpdateHunterStatusCall;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.HunterCallService;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceHunterOrderCallService;
import com.ymttest.database.model.hunter.HunterRelation;
import com.ymttest.database.sqlwapper.hunter.HunterRelationWrapper;
import com.ymttest.utils.DataUtil;

public class Ts_UpdateHunterStatus {
	
	private static UpdateHunterStatusRequest updateHunterStatusRequest;
	private static UpdateHunterStatusCall updateHunterStatusCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新hunter状态");
	}

	@Before
	public void caseUp() {
		updateHunterStatusRequest = new UpdateHunterStatusRequest();
		updateHunterStatusCall = new UpdateHunterStatusCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updateHunterStatusCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UpdateHunterStatus_001() {
		Logger.start(true, "封停hunter");
		try {
			
			// 创建订单
			Logger.comment("前置数据准备");
			
			OrderCallService orderCallService = new OrderCallService();
			int userId = orderCallService.createNewUser();
			long hunterId = 4321;
			TradingServiceResp placeOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterId, userId, 1);
			long tradingId = placeOrder.getTradingId();
			long orderId = placeOrder.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrder.getPayableAmount();

			Long thirdTradingId = TradingCallService.payOrder(userId, BigDecimal.ZERO, tradingId, 13);

			PayGateWayCallService.notifyCall("13", thirdTradingId.toString(),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId);
			
			Logger.comment("前置数据准备结束");
			
			Thread.sleep(3000);
			
			TradingSyncService.getSyncResult(orderId);
			
			updateHunterStatusRequest.setHunterId(userId);
			updateHunterStatusRequest.setStatus(1);

			updateHunterStatusCall.setData(updateHunterStatusRequest);
			updateHunterStatusCall.callService();
			
			Logger.verifyEquals("success", updateHunterStatusCall.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(0, updateHunterStatusCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(true, updateHunterStatusCall.getResponseData().getBoolean("success"),
					"验证success");
			
			HunterRelationWrapper wrapper = new HunterRelationWrapper();
			HunterRelation hunterRelation = wrapper.selectByPrimaryKey((long)userId);
			
			DataUtil.verify(1, hunterRelation.getStatus(),
					"验证hunterStatus");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UpdateHunterStatus_002() {
		Logger.start(true, "解封hunter");
		try {
			
			// 创建订单
			Logger.comment("前置数据准备");
			
			OrderCallService orderCallService = new OrderCallService();
			int userId = orderCallService.createNewUser();
			long hunterId = 4321;
			TradingServiceResp placeOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterId, userId, 1);
			long tradingId = placeOrder.getTradingId();
			long orderId = placeOrder.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrder.getPayableAmount();

			Long thirdTradingId = TradingCallService.payOrder(userId,
					BigDecimal.ZERO, tradingId, "13");

			PayGateWayCallService.notifyCall("13", thirdTradingId.toString(),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId);
			
			TradingSyncService.getSyncResult(orderId);
			
			Thread.sleep(3000);
			
			//封掉hunter
			HunterCallService.updateHunter(userId, 1);
			
			Logger.comment("前置数据准备结束");
			
			updateHunterStatusRequest.setHunterId(userId);
			updateHunterStatusRequest.setStatus(0);

			updateHunterStatusCall.setData(updateHunterStatusRequest);
			updateHunterStatusCall.callService();
			
			Logger.verifyEquals("success", updateHunterStatusCall.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(0, updateHunterStatusCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(true, updateHunterStatusCall.getResponseData().getBoolean("success"),
					"验证success");
			
			HunterRelationWrapper wrapper = new HunterRelationWrapper();
			HunterRelation hunterRelation = wrapper.selectByPrimaryKey(((long) userId));
			
			DataUtil.verify(0, hunterRelation.getStatus(),
					"验证hunterStatus");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UpdateHunterStatus_003() {
		Logger.start(true, "传入hunter为0");
		try {
			
			updateHunterStatusRequest.setHunterId(0);
			updateHunterStatusRequest.setStatus(0);

			updateHunterStatusCall.setData(updateHunterStatusRequest);
			updateHunterStatusCall.callService();
			
			Logger.verifyEquals("hunterId:推手用户编号不能为空", updateHunterStatusCall
					.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(2,
					updateHunterStatusCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(false, updateHunterStatusCall.getResponseData()
					.getBoolean("success"), "验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UpdateHunterStatus_004() {
		Logger.start(true, "推手状态值大于1");
		try {
			OrderCallService orderCallService = new OrderCallService();
			int userId = orderCallService.createNewUser();
			
			updateHunterStatusRequest.setHunterId(userId);
			updateHunterStatusRequest.setStatus(2);

			updateHunterStatusCall.setData(updateHunterStatusRequest);
			updateHunterStatusCall.callService();
			
			Logger.verifyEquals("status:推手状态值不能大于1", updateHunterStatusCall
					.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(2,
					updateHunterStatusCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(false, updateHunterStatusCall.getResponseData()
					.getBoolean("success"), "验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UpdateHunterStatus_005() {
		Logger.start(true, "hunter不存在");
		try {
			
			updateHunterStatusRequest.setHunterId(12314125141L);
			updateHunterStatusRequest.setStatus(1);

			updateHunterStatusCall.setData(updateHunterStatusRequest);
			updateHunterStatusCall.callService();
			
			Logger.verifyEquals("推手不存在或推手状态和待更新状态一致！", updateHunterStatusCall
					.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(3,
					updateHunterStatusCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(false, updateHunterStatusCall.getResponseData()
					.getBoolean("success"), "验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UpdateHunterStatus_006() {
		Logger.start(true, "多次封禁同一个hunter");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			OrderCallService orderCallService = new OrderCallService();
			int userId = orderCallService.createNewUser();
			long hunterId = 4321;
			TradingServiceResp placeOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterId, userId, 1);
			long tradingId = placeOrder.getTradingId();
			BigDecimal totalPrice = placeOrder.getPayableAmount();

			Long thirdTradingId = TradingCallService.payOrder(userId,
					BigDecimal.ZERO, tradingId, "13");

			PayGateWayCallService.notifyCall("13", thirdTradingId.toString(),
					totalPrice.subtract(BigDecimal.ZERO).toString(), userId);

			// 封掉hunter
			HunterCallService.updateHunter(userId, 1);

			Logger.comment("前置数据准备结束");
			
			updateHunterStatusRequest.setHunterId(userId);
			updateHunterStatusRequest.setStatus(1);

			updateHunterStatusCall.setData(updateHunterStatusRequest);
			updateHunterStatusCall.callService();
			
			Logger.verifyEquals("推手不存在或推手状态和待更新状态一致！", updateHunterStatusCall
					.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(3,
					updateHunterStatusCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(false, updateHunterStatusCall.getResponseData()
					.getBoolean("success"), "验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
